<?php

namespace App\Http\Controllers\Bend;


use App\Logics\StudentLogic;
use App\Models\User;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Redirect;

class ProfileController extends BaseController
{
    // 一般操作
    public function initialize()
    {
        parent::initialize();

        // 操作逻辑
        $this->model = 'User';
    }

    #################################################################################################
    public function onPassword()
    {
        $logic = $this->checkLogic($this->model);
        if($logic === false){
            return $this->error($logic->getError());
        }

        $result = $logic->onPassword($this->myUid);
        if($result === false){
            return $this->error($logic->getError());
        }

        // 删除cookie后需要重新定向
        //Cookie::queue(Cookie::make('organ_user', []));
        $this->optLogic()->request()->session()->put('organ_user', []);
        $cookie = Cookie::forget('organ_user');
        $url    = Url('bend/account/login');

        //return Redirect::secure($url)->withCookie($cookie);

        return $this->success($result, '修改密码成功，请重新登录！');
    }

    #################################################################################################
    // 修改密码
    public function password()
    {
        $form = $this->passwordFormHtml();
        $opt  = $this->optLogic();
        $opt->assign('formUrl', url('/bend/profile/on-password'));

        return $this->dispaly('form', ['formHtml' => $form]);
    }
    #################################################################################################

    // 修改密码：表单页面
    protected function passwordFormHtml()
    {
        $opt    = $this->optLogic();
        $manage = $this->checkLogic($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->info($this->myUid);
        $form = $this->adminForm();

        // 用户名
        $username  = $form->formShow($info['username'] ?: '-');
        $mobile    = $form->formShow($info['mobile'] ?: '-');
        $old_pass  = $form->formInput('old_pass', '', '旧密码');
        $new_pass  = $form->formInput('new_pass', '', '新密码');
        $new_pass2 = $form->formInput('new_pass2', '', '确认新密码');
        $_id       = $form->formHidden('id', $info['id'] ?? '0');

        $html .= $form->makeItem('用户名', $username);
        $html .= $form->makeItem('手机号', $mobile);
        $html .= $form->makeItem('旧密码', $old_pass);
        $html .= $form->makeItem('新密码', $new_pass);
        $html .= $form->makeItem('确认新密码', $new_pass2);
        $html .= $_id;

        return $html;
    }


    #################################################################################################
    ## 操作


    // 逻辑
    /*protected function checkLogic($logic = '')
    {
        $opt    = $this->optLogic();
        $action = $opt->getData('action', null, 'trim');
        $status = $opt->getData('status', null, 'intval');
        $opt->setField('myUid', $this->myUid);
        if(is_null($status) && in_array($action, ['lists', 'on-lists'])){
            $opt->setField('status', 999);
        }

        return new StudentLogic();
    }*/


}


